package com.lory.utils;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class GenUtils {

    public static void generatorCode(Map<String, Object> data, List<String> templates, ZipOutputStream zip) {
        //1.设置velocity的资源加载器
        Properties prop = new Properties();
        prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        //2.初始化velocity引擎
        Velocity.init(prop);
        //3.创建velocity容器
        VelocityContext context = new VelocityContext(data);
        //遍历模板列表，获取每一个模板，基于每一个模板生成一个代码
        for (String template: templates){
            //4. 加载velocity模板文件
            Template tpl = Velocity.getTemplate(template, "utf-8");
            //合并代码
            StringWriter sw = new StringWriter();
            tpl.merge(context, sw);
            //把代码数据合并输出到zip文件中
            try {
                zip.putNextEntry(new ZipEntry(getFileName(template, data.get("className").toString(), data.get("package").toString())));
                IOUtils.write(sw.toString(), zip, "UTF-8");
                IOUtils.closeQuietly(sw);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static String getFileName(String template, String className, String packageName){
        String packagePath = "main" + File.separator + "java" + File.separator;

        if (StringUtils.isNotEmpty(packageName)){
            packagePath += packageName.replace(".", File.separator) + File.separator;
        }
        if (template.contains("vms/Controller.java.vm")){
            return packagePath + "controller" + File.separator + className + "Controller.java";
        }

        if (template.contains("vms/Service.java.vm")){
            return packagePath + "service" + File.separator + className + "Service.java";
        }

        if (template.contains("vms/ServiceImpl.java.vm")){
            return packagePath + "service" + File.separator + "impl" + File.separator +  className + "ServiceImpl.java";
        }

        if (template.contains("vms/Dao.java.vm")){
            return packagePath + "dao" + File.separator + className + "Dao.java";
        }
        return null;
    }

    public static void main(String[] args) {
        String fileName = getFileName("vms/Controller.java.vm", "User", "com.lory");
        String fileName2 = getFileName("vms/Service.java.vm", "User", "com.lory");
        String fileName3 = getFileName("vms/ServiceImpl.java.vm", "User", "com.lory");
        String fileName4 = getFileName("vms/Dao.java.vm", "User", "com.lory");
        System.out.println(fileName);
        System.out.println(fileName2);
        System.out.println(fileName3);
        System.out.println(fileName4);
    }
}
